﻿using System;
using System.Web;

namespace QuickDev.SSO
{
    /// <summary>
    /// <para>RequestToken</para>
    /// </summary>
    [Serializable]
    public class RequestToken
    {
        #region Private Fields
        private string _returnUrl;
        private DateTime _timeStamp;
        private string _seed;
        #endregion

        #region Public Properties

        /// <summary>
        /// ReturnUrl
        /// </summary>
        public string ReturnUrl
        {
            get { return _returnUrl; }
            set { _returnUrl = value; }
        }

        /// <summary>
        /// TimeStamp
        /// </summary>
        public DateTime TimeStamp
        {
            get { return _timeStamp; }
            set { _timeStamp = value; }
        }

        /// <summary>
        /// Seed
        /// </summary>
        public string Seed
        {
            get { return _seed; }
            set { _seed = value; }
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// RequestToken
        /// </summary>
        /// <param name="returnUrl">returnUrl</param>
        /// <param name="timeStamp">timeStamp</param>
        /// <param name="seed">seed</param>
        public RequestToken( string returnUrl , DateTime timeStamp , string seed )
        {
            _returnUrl = returnUrl;
            _timeStamp = timeStamp;
            _seed = seed;
        }

        /// <summary>
        /// Encode
        /// </summary>
        /// <returns>HttpUtility.UrlEncode( encodedText )</returns>
        public string Encode()
        {
            string encodedText = SsoCipher.EncryptVector(
                _returnUrl ,
                _timeStamp == default( DateTime ) ? null : _timeStamp.ToString() ,
                _seed );
            return HttpUtility.UrlEncode( encodedText );
        }

        /// <summary>
        /// ToString
        /// </summary>
        /// <returns>Encode()</returns>
        public override string ToString()
        {
            return Encode();
        }

        /// <summary>
        /// TryParse
        /// </summary>
        /// <param name="tokenText">tokenText</param>
        /// <param name="token">token</param>
        /// <returns>bool successful</returns>
        public static bool TryParse( string tokenText , out RequestToken token )
        {
            token = null;
            if ( string.IsNullOrEmpty( tokenText ) )
                return false;
            string textToDecypt = HttpUtility.UrlDecode( tokenText );
            string[] vector = null;
            if ( !SsoCipher.TryParseVector( textToDecypt , out vector ) )
                return false;
            if ( vector.Length != 3 )
                return false;
            string returnUrl = vector[0];
            DateTime timeStamp = Convert.ToDateTime( vector[1] );
            string seed = vector[2];
            token = new RequestToken( returnUrl , timeStamp , seed );
            return true;
        }

        #endregion
    }
}